
ALTER proc [dbo].[CPP_User_Update_VisitMobile]
  @dwUserID INT,
  @szMobile NVARCHAR(11),       -- 手机号码
  @szUserName NVARCHAR(16),     -- 真实姓名
  @szUserCardID NVARCHAR(20),   -- 身份证号
  @strClientIP NVARCHAR(15),    -- IP地址
  @szResult NVARCHAR(255) OUTPUT  -- 输出信息
AS
BEGIN

  IF (ISNUMERIC(@szMobile)=0 OR LEN(@szMobile)<>11) BEGIN
    SET @szResult = '手机号码格式错误！'
    RETURN 1
  END
  
  IF LEN(@szUserName)=0 BEGIN
    SET @szResult = '真实姓名不能为空'
    RETURN 2
  END
  
  IF LEN(@szUserCardID)=0 BEGIN
    SET @szResult = '身份证号码不能为空'
    RETURN 3
  END

  DECLARE @UUID AS NVARCHAR(33) = ''
  DECLARE @Channel INT = 0
  SELECT @Channel = Channel, @UUID = UserUin FROM RYAccountsDB.dbo.AccountsInfo WHERE AccountsType = 0 AND UserID = @dwUserID
  IF @@ROWCOUNT=0 BEGIN
    SET @szResult = N'玩家[' + CAST(@dwUserID AS NVARCHAR) + ']不存在'
    RETURN 4
  END
  
  DECLARE @BindMobileFinsh AS INT = 0 -- 实名认证奖励金币数
  SELECT @BindMobileFinsh = ShiZengCoin FROM RYPlatformDB.dbo.GameChannel WHERE ID = @Channel
  IF @@ROWCOUNT=0 BEGIN
    SET @szResult = N'渠道[' + CAST(@Channel AS NVARCHAR) + ']不存在'
    RETURN 5
  END
  
  DECLARE @Total INT = 0
  SELECT @Total = COUNT(UserID) FROM RYAccountsDB.dbo.IndividualDatum WHERE IsBindPhone = 1 AND MobilePhone = @szMobile
  IF @Total>3 BEGIN
    SET @szResult = '1个手机号只能实名3个账号!'
    RETURN 6
  END
  
  DECLARE @OldMobile NVARCHAR(11) = ''
  DECLARE @IsBindPhone INT = 0
  SELECT @OldMobile = MobilePhone, @IsBindPhone = IsBindPhone FROM RYAccountsDB.dbo.IndividualDatum WHERE UserID = @dwUserID
  IF @@ROWCOUNT=0 BEGIN
    INSERT INTO [RYAccountsDB].[dbo].[IndividualDatum] ([UserID]) VALUES (@dwUserID);
  END
  
  IF (@OldMobile='' OR LEN(@OldMobile)<>11) BEGIN
    SET @IsBindPhone = 0
  END
  
  IF @IsBindPhone BEGIN
    SET @szResult = '您已完成实名认证!'
    RETURN 7
  END
  
  UPDATE RYAccountsDB.dbo.IndividualDatum SET MobilePhone = @szMobile, UserName = @szUserName, UserCardID = @szUserCardID, IsBindPhone = 1 WHERE UserID = @dwUserID
  
  DECLARE @DateID INT = CAST(CAST(GETDATE() AS FLOAT) AS INT)
  DECLARE @Score BIGINT = 0   -- 身上金币
  DECLARE @Insure BIGINT = 0  -- 保险箱金币
  DECLARE @TypeID = 8

  IF @BindMobileFinsh > 0 BEGIN

      -- 更新/插入/记录
      UPDATE RYAccountsDB.dbo.SystemGrantCount SET GrantScore=GrantScore+@BindMobileFinsh, GrantCount=GrantCount+1 WHERE DateID=@DateID AND RegisterIP=@strClientIP
      IF @@ROWCOUNT = 0 BEGIN
        INSERT RYAccountsDB.dbo.SystemGrantCount (DateID, RegisterIP, RegisterMachine, GrantScore, GrantCount) VALUES (@DateID, @strClientIP, N'', @BindMobileFinsh, 1)
      END
      -- 查询身上金币和保险箱金币
      SELECT @Score = Score, @Insure = InsureScore FROM RYTreasureDBLink.RYTreasureDB.dbo.GameScoreInfo WHERE UserID = @dwUserID
      IF @@ROWCOUNT = 0 BEGIN
        INSERT INTO [RYTreasureDB].[dbo].[GameScoreInfo] ([UserID], [RegisterIP], [LastLogonIP]) VALUES (@dwUserID, @strClientIP, @strClientIP);
      END ELSE BEGIN
        UPDATE [RYTreasureDB].[dbo].[GameScoreInfo] SET Score = Score + @BindMobileFinsh WHERE UserID = @dwUserID
      END

      -- 用户流水账
      INSERT INTO RYTreasureDBLink.RYTreasureDB.dbo.RecordPresentInfo(UserID,PreScore,PreInsureScore,PresentScore,TypeID,IPAddress)
      VALUES (@dwUserID,@Score, @Insure, @BindMobileFinsh, @TypeID, @strClientIP)

      -- 用户日统计
      UPDATE RYTreasureDBLink.RYTreasureDB.dbo.StreamPresentInfo SET
        DateID=@DateID, PresentCount=PresentCount+1,PresentScore=PresentScore+@BindMobileFinsh
      WHERE UserID=@dwUserID AND TypeID= @TypeID

      IF @@ROWCOUNT=0 BEGIN
        INSERT RYTreasureDBLink.RYTreasureDB.dbo.StreamPresentInfo(DateID,UserID,TypeID,PresentCount,PresentScore)
        VALUES(@DateID, @dwUserID, @TypeID, 1, @BindMobileFinsh)
      END

  END

  SELECT @Score = Score, @Insure = InsureScore FROM RYTreasureDBLink.RYTreasureDB.dbo.GameScoreInfo WHERE UserID = @dwUserID
  SELECT @dwUserID AS UserID, @Score AS Score, @Insure AS Insure, @UUID AS UserUin
  SET @szResult = '实名认证成功!'
  RETURN 0

END
